using IcsSEMCtrl.Db;
using IcsSEMCtrl.Global;
using IcsSEMCtrl.Global.Device;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
namespace IcsSEMCtrl.Util
{
	public class ObservableConfigGenerator
	{
		public class ObservableInfo
		{
			private string observableId;
			private AccessType accessType;
			private DataType dataType;
			public string ObservableId
			{
				get
				{
					return this.observableId;
				}
				set
				{
					this.observableId = value;
				}
			}
			public AccessType AccessType
			{
				get
				{
					return this.accessType;
				}
				set
				{
					this.accessType = value;
				}
			}
			public DataType DataType
			{
				get
				{
					return this.dataType;
				}
				set
				{
					this.dataType = value;
				}
			}
			public ObservableInfo(string observableId, AccessType accessType, DataType dataType)
			{
				this.observableId = observableId;
				this.accessType = accessType;
				this.dataType = dataType;
			}
		}
		private string savedDirName = string.Empty;
		public string SavedDirName
		{
			get
			{
				return this.savedDirName;
			}
			set
			{
				this.savedDirName = value;
			}
		}
		public ObservableConfigGenerator()
		{
			this.GenerateSubsystemConfigFiles();
		}
		public void GenerateEquipmentConfigFiles()
		{
		}
		public void GenerateModuleConfigFiles()
		{
		}
		public void GenerateSubsystemConfigFiles()
		{
			this.Generate(SubSystemType.STOCKERPORT);
			this.Generate(SubSystemType.CLEANERPORT);
			this.Generate(SubSystemType.PROCESSPORT);
			this.Generate(SubSystemType.RACKMASTER);
			this.Generate(SubSystemType.FFU);
			this.Generate(SubSystemType.SHELF);
			this.Generate(SubSystemType.LIFTERPORT);
			this.Generate(SubSystemType.VEHICLE);
			this.Generate(EquipmentType.STOCKER);
		}
		public void Generate(EquipmentType elementType)
		{
			string text = EquipmentType.ValueToString(elementType);
			string str = "StockerSEMConfig.cs";
			string sqlstring = "select distinct observableId, accesstype, dataType from observable \nwhere ownerid ='STOCKER'";
			string sqlstring2 = "select distinct classname from equipment where equipmenttype ='" + text + "'";
			StreamWriter streamWriter = null;
			if (this.savedDirName.Equals(string.Empty))
			{
				NameValueCollection appSettings = ConfigurationManager.AppSettings;
				this.savedDirName = appSettings[ECPProperties.ECP_BASE].ToString() + "\\temp";
			}
			if (!Directory.Exists(this.savedDirName))
			{
				Directory.CreateDirectory(this.savedDirName);
			}
			try
			{
				FileStream stream = new FileStream(this.savedDirName + "\\" + str, FileMode.Create, FileAccess.Write);
				BufferedStream stream2 = new BufferedStream(stream);
				streamWriter = new StreamWriter(stream2);
			}
			catch (IOException ex)
			{
				Console.WriteLine(ex.ToString());
			}
			string nameSpaceString = this.GetNameSpaceString(sqlstring2, text);
			this.WriteHeaderCode(streamWriter, nameSpaceString, "Stocker");
			List<ObservableConfigGenerator.ObservableInfo> observableList = this.GetObservableList(sqlstring);
			this.WriteObservableCode(streamWriter, text, observableList);
			this.WriteEndingCode(streamWriter, text);
			streamWriter.Close();
		}
		public void Generate(ModuleType moduleType)
		{
		}
		public void Generate(SubSystemType subSystemType)
		{
			string text = SubSystemType.ValueToString(subSystemType);
			string str = text + "Config.cs";
			string sqlstring = "select distinct observableId, accesstype, dataType, classname from observable a, subsystem b\nwhere a.ownerId = b.subsystemId and subsystemtype ='" + text + "'";
			string sqlstring2 = "select distinct classname from subsystem where subsystemtype ='" + text + "'";
			StreamWriter streamWriter = null;
			if (this.savedDirName.Equals(string.Empty))
			{
				NameValueCollection appSettings = ConfigurationManager.AppSettings;
				this.savedDirName = appSettings[ECPProperties.ECP_BASE].ToString() + "\\temp";
			}
			if (!Directory.Exists(this.savedDirName))
			{
				Directory.CreateDirectory(this.savedDirName);
			}
			try
			{
				FileStream stream = new FileStream(this.savedDirName + "\\" + str, FileMode.Create, FileAccess.Write);
				BufferedStream stream2 = new BufferedStream(stream);
				streamWriter = new StreamWriter(stream2);
			}
			catch (IOException ex)
			{
				Console.WriteLine(ex.ToString());
			}
			string nameSpaceString = this.GetNameSpaceString(sqlstring2, text);
			this.WriteHeaderCode(streamWriter, nameSpaceString, text);
			List<ObservableConfigGenerator.ObservableInfo> observableList = this.GetObservableList(sqlstring);
			this.WriteObservableCode(streamWriter, text, observableList);
			this.WriteEndingCode(streamWriter, text);
			streamWriter.Close();
		}
		public void WriteHeaderCode(StreamWriter writer, string nameSapce, string className)
		{
			string theData = string.Concat(new string[]
			{
				"using System;\nusing System.Collections.Generic;\nusing System.Text;\nusing IcsSEMCtrl.Device.Observe;\n\nnamespace ",
				nameSapce,
				"\n{\n\tpublic partial class ",
				className,
				"\n\t{\n\n"
			});
			this.WriteLine(writer, theData, className);
		}
		public void WriteObservableCode(StreamWriter writer, string fileName, List<ObservableConfigGenerator.ObservableInfo> observableInfos)
		{
			string text = "\t\t#region Properties for Observable Access\n";
			foreach (ObservableConfigGenerator.ObservableInfo current in observableInfos)
			{
				if (current.AccessType == AccessType.IN)
				{
					string text2 = text;
					text = string.Concat(new string[]
					{
						text2,
						"\t\tpublic Observable @__I_",
						current.ObservableId,
						"\n\t\t{\n\t\t\tget\n\t\t\t{\n\t\t\t\treturn this.Observables[\"",
						current.ObservableId,
						"\"];\n\t\t\t}\n\t\t}\n"
					});
				}
				if (current.AccessType == AccessType.OUT)
				{
					string text2 = text;
					text = string.Concat(new string[]
					{
						text2,
						"\t\tpublic Observable @__O_",
						current.ObservableId,
						"\n\t\t{\n\t\t\tget\n\t\t\t{\n\t\t\t\treturn this.Observables[\"",
						current.ObservableId,
						"\"];\n\t\t\t}\n\t\t}\n"
					});
				}
				if (current.AccessType == AccessType.IN_OUT)
				{
					string text2 = text;
					text = string.Concat(new string[]
					{
						text2,
						"\t\tpublic Observable @__IO_",
						current.ObservableId,
						"\n\t\t{\n\t\t\tget\n\t\t\t{\n\t\t\t\treturn this.Observables[\"",
						current.ObservableId,
						"\"];\n\t\t\t}\n\t\t}\n"
					});
				}
			}
			text += "\t\t#endregion\n\n\t\t#region Properties for Observable Access By Value\n\n";
			foreach (ObservableConfigGenerator.ObservableInfo current in observableInfos)
			{
				if (current.AccessType == AccessType.IN)
				{
					string text2 = text;
					text = string.Concat(new string[]
					{
						text2,
						"\t\tpublic ",
						this.GetPrimitiveTypeString(current.DataType),
						" I_",
						current.ObservableId,
						"\n"
					});
				}
				if (current.AccessType == AccessType.OUT)
				{
					string text2 = text;
					text = string.Concat(new string[]
					{
						text2,
						"\t\tpublic ",
						this.GetPrimitiveTypeString(current.DataType),
						" O_",
						current.ObservableId,
						"\n"
					});
				}
				if (current.AccessType == AccessType.IN_OUT)
				{
					string text2 = text;
					text = string.Concat(new string[]
					{
						text2,
						"\t\tpublic ",
						this.GetPrimitiveTypeString(current.DataType),
						" IO_",
						current.ObservableId,
						"\n"
					});
				}
				text += "\t\t{\n";
				if (current.AccessType == AccessType.IN || current.AccessType == AccessType.IN_OUT)
				{
					string text2 = text;
					text = string.Concat(new string[]
					{
						text2,
						"\t\t\tget\n\t\t\t{\n\t\t\t\treturn this.Observables[\"",
						current.ObservableId,
						"\"]",
						this.GetCustomTypeString(current.DataType),
						";\n\t\t\t}\n"
					});
				}
				if (current.AccessType == AccessType.OUT || current.AccessType == AccessType.IN_OUT)
				{
					string text2 = text;
					text = string.Concat(new string[]
					{
						text2,
						"\t\t\tset\n\t\t\t{\n\t\t\t\tthis.Observables[\"",
						current.ObservableId,
						"\"]",
						this.GetCustomTypeString(current.DataType),
						" = value;\n\t\t\t}\n"
					});
				}
				text += "\t\t}\n";
			}
			text += "\t\t#endregion\n";
			this.WriteLine(writer, text, fileName);
		}
		public void WriteEndingCode(StreamWriter writer, string fileName)
		{
			string text = "";
			text += "\t}\n}\n";
			this.WriteLine(writer, text, fileName);
		}
		public List<ObservableConfigGenerator.ObservableInfo> GetObservableList(string sqlstring)
		{
			List<ObservableConfigGenerator.ObservableInfo> list = new List<ObservableConfigGenerator.ObservableInfo>();
			DBConnection connection = DBControl.GetConnection();
			SqlDataReader sqlDataReader = connection.ExecuteQuery(sqlstring);
			while (sqlDataReader.Read())
			{
				list.Add(new ObservableConfigGenerator.ObservableInfo((string)sqlDataReader["OBSERVABLEID"], AccessType.StringToValue((string)sqlDataReader["ACCESSTYPE"]), DataType.StringToValue((string)sqlDataReader["DATATYPE"])));
			}
			if (sqlDataReader != null)
			{
				sqlDataReader.Close();
			}
			DBControl.RemoveDBConection(connection);
			return list;
		}
		public string GetNameSpaceString(string sqlstring, string classname)
		{
			string text = null;
			DBConnection connection = DBControl.GetConnection();
			SqlDataReader sqlDataReader = connection.ExecuteQuery(sqlstring);
			if (sqlDataReader.Read())
			{
				text = (string)sqlDataReader["CLASSNAME"];
			}
			if (text != null)
			{
				text = text.Substring(0, text.IndexOf("." + classname));
			}
			if (sqlDataReader != null)
			{
				sqlDataReader.Close();
			}
			DBControl.RemoveDBConection(connection);
			return text;
		}
		private void WriteLine(StreamWriter writer, string theData, string theFileName)
		{
			try
			{
				writer.Write(theData);
				Console.Write(theData);
			}
			catch (IOException ex)
			{
				Console.WriteLine(ex.ToString());
			}
		}
		public string GetPrimitiveTypeString(DataType dataType)
		{
			string result = "";
			switch (dataType.Value())
			{
			case 0:
				result = "bool";
				break;
			case 1:
				result = "byte";
				break;
			case 2:
				result = "short";
				break;
			case 3:
				result = "int";
				break;
			case 4:
				result = "long";
				break;
			case 5:
				result = "double";
				break;
			case 6:
				result = "string";
				break;
			case 7:
				result = "List<short>";
				break;
			case 11:
				result = "List<bool>";
				break;
			}
			return result;
		}
		public string GetCustomTypeString(DataType dataType)
		{
			string result = "";
			switch (dataType.Value())
			{
			case 0:
				result = ".AsBoolean";
				break;
			case 1:
				result = ".AsByte";
				break;
			case 2:
				result = ".AsShort";
				break;
			case 3:
				result = ".AsInteger";
				break;
			case 4:
				result = ".AsLong";
				break;
			case 5:
				result = ".AsDouble";
				break;
			case 6:
				result = ".AsString";
				break;
			case 7:
				result = ".AsShortArray";
				break;
			case 11:
				result = ".AsBoolArray";
				break;
			}
			return result;
		}
	}
}
